import { specslist, specscount } from '../../request/api';

export default {
    namespaced: true,
    state: {
        specsList: [],
        specsCount: 0,
        pageData: {
            page: 1,
            size: 1,
        },
    },
    getters: {
    },
    mutations: {
        changeSpecsList(state, data) {
            state.specsList = data;

        },
        changeSpecsCount(state, data) {
            state.specsCount = data;
        },
        changePageData(state, data) {
            state.pageData.page = data;
        }
    },
    actions: {
        getSpecsList(context, a) {
            specslist(a ? {} : context.state.pageData).then(res => {
                if (res.code == 200) {
                    let list = res.list ? res.list : [];
                    if (list.length == 0 && context.state.pageData.page != 1) {
                        context.state.pageData.page = context.state.pageData.page - 1;
                        context.dispatch('getSpecsList');
                        return;
                    }
                    context.commit('changeSpecsList', list);
                }
            })
        },
        getSpecsCount(context, a) {
            specscount().then(res => {
                if (res.code == 200) {
                    context.commit('changeSpecsCount', res.list[0].total);
                    if (a) {
                        context.commit('changePageData', Math.ceil(context.state.specsCount / context.state.pageData.size));
                    }
                    context.dispatch('getSpecsList');
                }
            })
        }
    },
    modules: {
    }
}